<template>
	<view>
		<view style="text-align: center;margin-bottom: 20rpx;">
			已配对蓝牙设备
		</view>
		<view style="text-align: center;padding: 10rpx 0;border: 1rpx solid #2B85E4;border-radius: 15rpx;"
			class="uni-flex uni-row" v-for="(device,index) in deviceList" :key="index">
			<view style="width: 30rpx;">{{index+1}}</view>
			<view style="width: 180rpx;">{{device.name}}</view>
			<view style="width: 310rpx;">{{device.address}}</view>
			<view class="flex-item">
				<button type="primary" plain size="mini" @click="printSomething(device,printTest)">打印测试</button>
			</view>
		</view>
	</view>

</template>

<script>
	// import Tsc from '@/utils/gprint/tsc.js'
	import Tspl from '@/utils/print/Tspl.class.js'
	// const encode = require("@/utils/gprint/encoding.js");
	import {
		encode as encoder
	} from "@/utils/print/GBKCodec";
	// import {encode, decodeText} from '@/utils/print/GBKCodec.js'
	import panels from "./panels"
	const dot = (pt) => {
		return Math.floor((pt / 2.815) * 8)
	}
	const setLevel = (level) => {
		switch (level) {
			case '1':
				level = 'L'
				break;
			case '2':
				level = 'H'
				break;
			case '3':
				level = 'Q'
				break;
			default:
				level = 'M'
				break;
		}
		return level
	}
	export default {
		data() {
			return {
				show: {
					setting: false
				},
				deviceList: []
			}
		},
		onLoad(p) {
			var that = this;
			// #ifdef APP-PLUS
			that.initPrinter();
			// #endif
		},
		methods: {
			showPrinterList: function() {
				var that = this;
				that.show.setting = true;
				for (var i = 0; i < that.deviceList.length; i++) {
					if (that.deviceList[i].name == that.device.name) {
						that.deviceList[i].checked = true
					}
				}
			},
			initPrinter: function() {
				var that = this;
				that.deviceList = [];
				var main = plus.android.runtimeMainActivity();
				var Context = plus.android.importClass("android.content.Context");
				var BManager = main.getSystemService(Context.BLUETOOTH_SERVICE);
				plus.android.importClass(BManager);
				var BAdapter = BManager.getAdapter();
				plus.android.importClass(BAdapter);
				var lists = BAdapter.getBondedDevices();
				plus.android.importClass(lists);
				var iterator = lists.iterator();
				plus.android.importClass(iterator);
				while (iterator.hasNext()) {
					var d = iterator.next();
					plus.android.importClass(d);
					var temp = {
						name: d.getName(),
						address: d.getAddress(),
						status: d.getBondState(),
						uuids: d.getUuids(),
						op: d
					};
					that.deviceList.push(temp);
				}
			},
			printSomething: function(dev, sb) {
				var that = this;
				var main = plus.android.runtimeMainActivity();
				var BluetoothAdapter = plus.android.importClass("android.bluetooth.BluetoothAdapter");
				var UUID = plus.android.importClass("java.util.UUID");
				var uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
				var BAdapter = BluetoothAdapter.getDefaultAdapter();
				var device = BAdapter.getRemoteDevice(dev.address);
				plus.android.importClass(device);
				var bluetoothSocket = device.createInsecureRfcommSocketToServiceRecord(uuid);
				plus.android.importClass(bluetoothSocket);
				console.log("开始连接打印机:" + dev.name);
				if (!bluetoothSocket.isConnected()) {
					bluetoothSocket.connect();
					if (bluetoothSocket.isConnected()) {
						console.log("设备已连接,开始发送打印文件");
						var outputStream = bluetoothSocket.getOutputStream();
						plus.android.importClass(outputStream);
						sb(outputStream);
						bluetoothSocket.close();
						if (!bluetoothSocket.isConnected()) {
							console.log("设备已关闭");
						}
					} else {
						uni.showToast({
							title: '设备连接失败',
							icon: 'error',
							duration: 2000
						});
					}
				}
			},
			printTest: function(outputStream) {
				const source = this.getTscCommand()
				console.log(source);
				var arrayBuffer = plus.android.invoke(source, 'getBytes', 'gbk');
				console.log(arrayBuffer);
				outputStream.write(arrayBuffer);
				outputStream.flush();

			},

			getTscCommand() {
				const tspl = new Tspl({
					size: `${panels.width} mm, ${panels.height} mm`,
					gap: " 2mm, 0",
				})
				panels.printElements.map((item) => {
					if (item.printElementType.type === 'text') {
						switch (item.options.textType) {
							case 'qrcode':
							case 'barcode':
								item.printElementType.type = item.options.textType
								break
						}
					}
				})
				panels.printElements.map((item) => {
					console.log(item);
					console.log(item);
					let {
						left,
						top,
						width,
						height,
						fontSize,
						field,
						title,
						hideTitle,
						textAlign, 
						textContentVerticalAlign,
						fontWeight,
						lineHeight,
						testData,
						barcodeMode,
						qrCodeLevel
					} = item.options
					left = dot(left)
					top = dot(top)
					width = dot(width)
					height = dot(height)
					lineHeight = dot(lineHeight || fontSize || 9)
					// console.log(
					//     left,
					//     top,
					//     width,
					//     height,
					//     fontSize,
					//     field,
					//     title,
					//     hideTitle,
					//     textAlign,
					//     textContentVerticalAlign,
					//     fontWeight,
					//     lineHeight,
					//     testData,
					//     barcodeMode,
					//     qrCodeLevel);

					switch (item.printElementType.type) {
						case 'text':
							tspl.text(left, top,
								`${hideTitle ? '' : title || ''}${!hideTitle && field ? ':' : ''}${testData || ''}`, 1
							)
							break;
						// case 'longText':
						// 	tspl.block(left, top, width, height,
						// 		`${hideTitle ? '' : title || ''}${!hideTitle && field ? ':' : ''}${testData || ''}`
						// 	)
						// 	break;
						case 'qrcode':
							tspl.qrcode(left, top, testData)
							break;
						case 'barcode':
							tspl.barcode(left, top, height, testData);
							break;
					}
				})

				return tspl._export()
			}
		}
	}
</script>

<style scoped>
	page {
		padding: 0;
		margin: 0;
	}

	.picked {
		background-color: lavender;
	}
</style>